package cn.org.farseer.sweetftp.core;

import java.io.File;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import cn.org.farseer.sweetftp.core.persistent.IPersistent;
import cn.org.farseer.sweetftp.core.persistent.PersistentConfigure;
import cn.org.farseer.sweetftp.core.support.CustomService;
import cn.org.farseer.sweetftp.core.support.IPersistentOperationSupport;
import cn.org.farseer.sweetftp.core.util.SweetFTPConstants;

/**
 * 作者 : [Farseer.org] 简洋  —  2009-8-21
 * 
 * 项目 : [Farseer.org] SweetFTP
 *
 * 说明：
 */
@CustomService
public class FTPContext
{
	private static FTPContext context;
	private FTPConfiguration configuration;
	private static final Log logger = LogFactory.getLog(FTPContext.class);
	private IPersistent persistent;
	private PersistentConfigure persistentConfigure;
	private List<IPersistentOperationSupport> registeredArray;
	
	/**
	 * 获得实例的方法。
	 * @return
	 */
	public static FTPContext getInstance()
	{
		return context;
	}
	
	
	/**
	 * 初始化方法。
	 * @param configuration
	 */
	public void init()
	{
		context = this;
		
		//初始化持久化组件的基础信息。
		String persistentBasePath = System.getProperty(SweetFTPConstants.USER_DIR) + File.separator + SweetFTPConstants.PERSISTENT_FOLDER_NAME;
		logger.debug("persistentBasePath 持久化基础目录为 ：" + persistentBasePath);
		File persistentBasePathDir = new File(persistentBasePath);
		if(!persistentBasePathDir.exists())
			persistentBasePathDir.mkdirs();
		
		persistentConfigure = new PersistentConfigure(persistentBasePath);
		persistent.setConfigure(persistentConfigure);
		
		initLoadData();
	}
	
	/**
	 * 加载硬盘上持久化的信息。
	 */
	private void initLoadData()
	{
		//对所有注册的持久化对象进行加载操作。
		logger.debug("开始加载持久化对象----------------------");
		for (Iterator<IPersistentOperationSupport>  iterator = registeredArray.iterator(); iterator.hasNext();)
		{
			try{
				IPersistentOperationSupport support = (IPersistentOperationSupport) iterator.next();
				support.load(persistent);
				logger.debug("持久化类的加载完成 ： " + support.getClass().getName());
			}catch(Exception e)
			{
				logger.error(e);
			}
		}
		logger.debug("完成加载持久化对象----------------------");
	}
	
	
	
	public void destroy()
	{
		System.out.println("destory~");
	}
	
	
	
	
	
	
	
	
	public FTPConfiguration getConfiguration()
	{
		return configuration;
	}

	public void setConfiguration(FTPConfiguration configuration)
	{
		this.configuration = configuration;
	}

	@Resource(type=IPersistent.class)
	public void setPersistent(IPersistent persistent)
	{
		this.persistent = persistent;
	}

	public List<IPersistentOperationSupport> getRegisteredArray()
	{
		return registeredArray;
	}
	
	@Autowired
	public void setRegisteredArray(List<IPersistentOperationSupport> registeredArray)
	{
		this.registeredArray = registeredArray;
	}
	
}
